public class Solution {
    public static int Multiply(int[] p){
        int[][] dp = new int[p.length][p.length];
        for(int i=1; i<p.length;i++){
            dp[i][i] = 0;
        }
        for(int m=1;m<=p.length-2;m++){
            for(int i=1;i<=p.length-1-m;i++){
                int j = i+m;
                for(int k=i;k<j;k++){
                    dp[i][j] = dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j];
                }

            }
        }
       return dp[1][p.length-1];
    }

    public static void main(String args[]){
        int[] p = {10,20,30,40,20,30};
        int a = Multiply(p);
        System.out.println(a);
    }

}

